package com.dyh.quickdemozwei.domain;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class CommonResult<T> {
    private static final int SUCC = 200;
    private static final int FAIL = 400;
    private static final int UNKNOW = -1;
    private int code;
    private T data;
    private String message;
    private String key;


    public CommonResult() {
    }

    public CommonResult(int code, T data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    public static CommonResult succ() {
        CommonResult view = new CommonResult();
        view.code = 200;
        view.message = "请求成功";
        return view;
    }



    public static <T> CommonResult<T> succ(T data) {
        CommonResult view = new CommonResult();
        view.code = 200;
        view.message = "请求成功";
        view.setData(data);
        return view;
    }


    public static <T> CommonResult<T> succ(String key,T data) {
        CommonResult view = new CommonResult();
        view.code = 200;
        view.key = key;
        view.message = "请求成功";
        view.setData(data);
        return view;
    }


    public static CommonResult fail() {
        CommonResult view = new CommonResult();
        view.code = 400;
        return view;
    }

    public static CommonResult fail(String message) {
        CommonResult view = new CommonResult();
        view.code = 400;
        view.message = message;
        return view;
    }

    public static CommonResult fail(String key,String message) {
        CommonResult view = new CommonResult();
        view.code = 400;
        view.key = key;
        view.message = message;
        return view;
    }


    public static CommonResult fail(Long key,String message) {
        CommonResult view = new CommonResult();
        view.code = 400;
        view.key = String.valueOf(key);
        view.message = message;
        return view;
    }

    public static CommonResult fail(StringBuilder stringBuilder) {
        CommonResult view = new CommonResult();
        view.code = 400;
        view.message = stringBuilder.toString();
        return view;
    }

    public static CommonResult unknow(String message) {
        CommonResult view = new CommonResult();
        view.code = UNKNOW;
        view.message = message;
        return view;
    }

    public  boolean isSuccess(){
        return SUCC == this.code;
    }
}